<template>
  <common-layout>
    <div class="top">
      <div class="header">
        <img alt="logo" class="logo" src="@/assets/img/logo.png" />
        <span class="title">{{ systemName }}</span>
      </div>
      <div class="desc">
        SIOT是一个轻量级的物联网平台，简单配置即可对接您的设备
      </div>
    </div>
    <div class="login">
      <a-tabs
        @change="tabChange"
        size="large"
        :tabBarStyle="{ textAlign: 'center' }"
        style="padding: 0 2px"
      >
        <a-tab-pane tab="租户" key="1">
          <a-form @submit="userOnSubmit" :form="form">
            <a-alert
              type="error"
              :closable="true"
              v-show="error"
              :message="error"
              showIcon
              style="margin-bottom: 24px"
            />
            <a-form-item>
              <a-input
                autocomplete="autocomplete"
                size="large"
                placeholder="租户标识"
                v-decorator="[
                  'tenantAlias',
                  {
                    rules: [
                      {
                        required: true,
                        message: '请输入租户标识',
                        whitespace: true,
                      },
                    ],
                  },
                ]"
              >
                <a-icon slot="prefix" type="user" />
              </a-input>
            </a-form-item>
            <a-form-item>
              <a-input
                autocomplete="autocomplete"
                size="large"
                placeholder="账户名"
                v-decorator="[
                  'userName',
                  {
                    rules: [
                      {
                        required: true,
                        message: '请输入账户名',
                        whitespace: true,
                      },
                    ],
                  },
                ]"
              >
                <a-icon slot="prefix" type="user" />
              </a-input>
            </a-form-item>
            <a-form-item>
              <a-input
                size="large"
                placeholder="密码"
                autocomplete="autocomplete"
                type="password"
                v-decorator="[
                  'password',
                  {
                    rules: [
                      {
                        required: true,
                        message: '请输入密码',
                        whitespace: true,
                      },
                    ],
                  },
                ]"
              >
                <a-icon slot="prefix" type="lock" />
              </a-input>
            </a-form-item>
            <a-form-item>
              <a-button
                :loading="logging"
                style="width: 100%; margin-top: 24px"
                size="large"
                htmlType="submit"
                type="primary"
                >登录</a-button
              >
            </a-form-item>
          </a-form>
        </a-tab-pane>
        <a-tab-pane tab="管理员" key="2">
          <a-form @submit="adminOnSubmit" :form="form2">
            <a-form-item>
              <a-input
                v-decorator="[
                  'nickName',
                  {
                    rules: [
                      {
                        required: true,
                        message: '请输入账号',
                        whitespace: true,
                      },
                    ],
                  },
                ]"
                size="large"
                placeholder="账号"
              >
                <a-icon slot="prefix" type="user" />
              </a-input>
            </a-form-item>
            <a-form-item>
              <a-input
                v-decorator="[
                  'adminPwd',
                  {
                    rules: [
                      {
                        required: true,
                        message: '请输入密码',
                        whitespace: true,
                      },
                    ],
                  },
                ]"
                size="large"
                placeholder="密码"
              >
                <a-icon slot="prefix" type="lock" />
              </a-input>
            </a-form-item>
            <a-form-item>
              <a-button
                :loading="logging"
                style="width: 100%; margin-top: 24px"
                size="large"
                htmlType="submit"
                type="primary"
                >登录</a-button
              >
            </a-form-item>
          </a-form>
        </a-tab-pane>
      </a-tabs>
      <!-- <div> -->
      <!-- <a-checkbox :checked="true" >自动登录</a-checkbox> -->
      <!-- <a style="float: right">忘记密码</a> -->
      <!-- </div> -->

      <!-- <div> -->
      <!-- 其他登录方式
          <a-icon class="icon" type="alipay-circle" />
          <a-icon class="icon" type="taobao-circle" />
          <a-icon class="icon" type="weibo-circle" /> -->
      <!-- <router-link style="float: right" to="/dashboard/workplace" >注册账户</router-link> -->
      <!-- </div> -->
    </div>
  </common-layout>
</template>

<script>
import CommonLayout from "@/layouts/CommonLayout";
import { tenantLogin, adminLogin, getRoutesConfig } from "@/services/auth";
import { setAuthorization } from "@/utils/request";
import { loadRoutes } from "@/utils/routerUtil";
import { mapMutations } from "vuex";

export default {
  name: "Login",
  components: { CommonLayout },
  data() {
    return {
      logging: false,
      error: "",
      form: this.$form.createForm(this),
      form2: this.$form.createForm(this),
      curTabId: 1,
    };
  },
  computed: {
    systemName() {
      return this.$store.state.setting.systemName;
    },
  },
  methods: {
    ...mapMutations("account", ["setUser", "setPermissions", "setRoles"]),
    tabChange(key) {
      console.log("change", key);
    },
    userOnSubmit(e) {
      e.preventDefault();
      this.form.validateFields((err) => {
        if (!err) {
          this.logging = true;
          const tenantAlias = this.form.getFieldValue("tenantAlias");
          const userName = this.form.getFieldValue("userName");
          const password = this.form.getFieldValue("password");
          tenantLogin({
            tenantAlias: tenantAlias,
            userName: userName,
            password: password,
          }).then(this.afterLogin);
        }
      });
    },
    adminOnSubmit(e) {
      e.preventDefault();
      console.log(this.form);
      console.log(this.form2);
      this.form2.validateFields((err) => {
        if (!err) {
          this.logging = true;
          const nickName = this.form2.getFieldValue("nickName");
          const password = this.form2.getFieldValue("adminPwd");
          adminLogin({
            nickName: nickName,
            password: password,
          }).then(this.afterLogin);
        }
      });
    },
    afterLogin(res) {
      this.logging = false;
      const loginRes = res.data;
      if (loginRes.code == 0) {
        const user = {};
        user.name = loginRes.data.userName;
        this.setUser(user);
        // this.setPermissions(permissions);
        this.setRoles([loginRes.data.userType]);
        setAuthorization(
          loginRes.data.token,
          new Date(new Date().getTime() + 3600 * 60 * 1000)
        );
        // 获取路由配置
        getRoutesConfig(1).then((result) => {
          const routesConfig = result.data.data;
          loadRoutes(routesConfig);
          if (loginRes.data.userType === "tenant")
            this.$router.push("/datadashbord");
          else if (loginRes.data.userType === "admin")
            this.$router.push("/admindatadashbord");
          this.$message.success("登录成功", 3);
        });
      } else {
        this.$message.error(loginRes.msg, 3);
      }
    },
  },
};
</script>

<style lang="less" scoped>
.common-layout {
  .top {
    text-align: center;
    .header {
      height: 44px;
      line-height: 44px;
      a {
        text-decoration: none;
      }
      .logo {
        height: 44px;
        vertical-align: top;
        margin-right: 16px;
      }
      .title {
        font-size: 33px;
        color: @title-color;
        font-family: "Myriad Pro", "Helvetica Neue", Arial, Helvetica,
          sans-serif;
        font-weight: 600;
        position: relative;
        top: 2px;
      }
    }
    .desc {
      font-size: 14px;
      color: @text-color-second;
      margin-top: 12px;
      margin-bottom: 40px;
    }
  }
  .login {
    width: 368px;
    margin: 0 auto;
    @media screen and (max-width: 576px) {
      width: 95%;
    }
    @media screen and (max-width: 320px) {
      .captcha-button {
        font-size: 14px;
      }
    }
    .icon {
      font-size: 24px;
      color: @text-color-second;
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: @primary-color;
      }
    }
  }
}
</style>
